<template>
  <div id="detail">
    <detail-nav-bar class="detail-nav" ref="Nav" @titlesClick="titlesClick"></detail-nav-bar>
    <scroll class="content" ref="scroll" :probe-type-num="3" @scroll="contentScroll">
      <detail-swiper :top-images="topItems"></detail-swiper>
      <detail-base-info :good="good"></detail-base-info>
      <detail-shop-info :shop="shop"></detail-shop-info>
      <detail-goods-info :detailInfo="detailInfo" @imgLoad="imgLoad"></detail-goods-info>
      <detail-param-info ref="param" :param-info="paramInfo"></detail-param-info>
      <detail-comment-info ref="comment" :commentInfo="commentInfo"></detail-comment-info>
      <goods-list ref="recommend" :goods="recommend"></goods-list>
    </scroll>
    <back-top @click.native="backClick" v-show="backTopShow"></back-top>

    <detail-bottom-bar @addToCart="addToCart"></detail-bottom-bar>
  </div>
</template>

<script>
import DetailNavBar from "./childComps/DetailNavBar";
import DetailSwiper from "./childComps/DetailSwiper";
import DetailBaseInfo from "./childComps/DetailBaseInfo";
import DetailShopInfo from "./childComps/DetailShopInfo";
import Scroll from "../../components/common/scroll/Scroll";
import DetailGoodsInfo from "./childComps/DetailGoodsInfo";
import DetailParamInfo from "./childComps/DetailParamInfo";
import DetailCommentInfo from "./childComps/DetailCommentInfo";
import GoodsList from "../../components/content/goods/GoodsList";
import DetailBottomBar from "./childComps/DetailBottomBar";


import {getDetail, Goods, Shop, GoodsParam, getRecommend} from "../../network/detail";
import {itemListenerMixin,backTop} from "../../common/mixin";


export default {
  name: "Detail",
  components: {
    GoodsList,
    DetailGoodsInfo,
    DetailNavBar,
    DetailSwiper,
    DetailBaseInfo,
    DetailShopInfo,
    Scroll,
    DetailParamInfo,
    DetailCommentInfo,
    DetailBottomBar,
  },
  mixins: [itemListenerMixin,backTop]
  ,
  data() {
    return {
      iid: null,
      topItems: [],
      good: {},
      shop: {},
      detailInfo: {},
      paramInfo: {},
      commentInfo: {},
      recommend: [],
      themeTopYs: [0, 100, 1000, 2000],
      currentIndex: 0,

    }
  }, created() {
    //保存传入的iid
    this.iid = this.$route.params.iid;
    //根据iid请求详情数据
    getDetail(this.iid).then((res) => {
     // console.log(res)
      const data = res.result;
      this.topItems = data.itemInfo.topImages;
      //获取商品信息
      this.good = new Goods(data.itemInfo, data.columns, data.shopInfo.services)
      //获取店铺信息
      this.shop = new Shop(data.shopInfo)
      //获取商品详情
      this.detailInfo = data.detailInfo;
      //获取商品参数
      this.paramInfo = new GoodsParam(data.itemParams.info, data.itemParams.rule)
      //获取商品评论
      if (data.rate.cRate !== 0) {
        this.commentInfo = data.rate.list[0]
      }
      //获取推荐商品
      getRecommend().then((res) => {
        this.recommend = res.data.list
        // console.log(this.recommend)
      })
    })
  },
  destroyed() {
    this.$bus.$off("ImageLoad", this.itemImgListener)
  },
  methods: {
    imgLoad() {
      this.$refs.scroll.refresh()

      //给导航栏设置Y坐标
      this.themeTopYs = []
      this.themeTopYs.push(0);
      this.themeTopYs.push(this.$refs.param.$el.offsetTop)
      this.themeTopYs.push(this.$refs.comment.$el.offsetTop)
      this.themeTopYs.push(this.$refs.recommend.$el.offsetTop)
    },
    //导航栏跳转
    titlesClick(index) {
      this.$refs.scroll.scrollTo(0, -this.themeTopYs[index], 100)
    },
    contentScroll(position) {
      // console.log(position)
      const positionY = -position.y
      for (let i = 0; i < this.themeTopYs.length; i++) {
        if (this.currentIndex !== i && (
            (positionY >= this.themeTopYs[i] && positionY < this.themeTopYs[i + 1]
            ) || (i === this.themeTopYs.length - 1 && positionY >= this.themeTopYs[i]
            ))) {
          this.currentIndex = i
          this.$refs.Nav.currentIndex = this.currentIndex
        }
      }
      this.backTopShow = (positionY) > 1000;

    },
    addToCart(){
      const product={}
      product.image=this.topItems[0];
          product.title=this.good.title;
          product.desc=this.good.desc;
          product.price=this.good.lowNowPrice;
          product.iid=this.iid

      this.$store.dispatch("addCart",product)
    }

  }


}
</script>

<style scoped>
#detail {
  position: relative;
  z-index: 9;
  background-color: #f6f6f6;
  height: 100vh;
}

.detail-nav {
  position: relative;
  z-index: 9;
  background-color: #f6f6f6;
}

.content {
  height: calc(100% - 44px - 49px);
  overflow: hidden;
}

</style>